define(['jquery','treeView'],function($){
    'use strict';
	angular.module('usercenter.sysAction', ['app.treeView'])
    .controller('sysActionCtrl'
			,['$rootScope', '$scope' ,  '$uibModal', 'BizData', '$ocLazyLoad'
                                   ,f])
       	function f($rootScope,$scope ,  $uibModal, BizData, $ocLazyLoad
        ) {
    		$scope.headerText = '----------我是系统客户区sysActionCtrl控制器！id为-->'+$scope.$id+'开始----------';
    		$scope.footerText = '----------我是系统客户区sysActionCtrl控制器！id为-->'+$scope.$id+'结束----------';
    		

    		var Model = this;
       		
       		$scope.appId = 'appId';

    		/**
    		name:bizData#onBeforeDelete
    		description: <b>[回调型事件]</b>业务数据删除前
    		@param {object} event 它的结构如下:<br/>
    		{
    			"source":组件的js对象
    			,"cancel":可修改	，设置为true后中断当前delete动作
    			,"deleteIDList":删除的行Id数组
    		}
    		*/
    		Model.dataSysInfoBeforeDelete = function(event) {
    			
    		}
    		/**
    		name:bizData#onAfterDelete
    		description: <b>[回调型事件]</b>业务数据删除后
    		@param {object} event 
    		<br/><b>结构如下：</b>
    		<xmp> 
    		{
    			"source" : 组件的js对象,
    			"deleteIDList" : 删除的行Id数组
    		}
    		</xmp>	
    		*/
    		Model.dataSysInfoAfterDelete = function(event) {
//                            			alert('删除之后！')
    		}

    		
    		
    		/**
    		name:bizData#onIndexChanging
    		description: <b>[回调型事件]</b>行记录变化中
    		@param {object} event 
    		<br/><b>结构如下：</b>
    		<xmp> 
    		{
    			"source" : 组件的js对象,
    			"rowID" : 行Id,
    			"rowIndex" : 行索引,
    			"originalRowID" : 原行Id,
    			"originalRowIndex" : 原行索引
    		}
    		</xmp>	
    		*/
    		Model.dataSysInfoIndexChanging = function(event){
    			
    		};
    		
    		
    		/**
    		name:bizData#onIndexChanged
    		description: <b>[回调型事件]</b>行记录变化
    		@param {object} event 
    		<br/><b>结构如下：</b>
    		<xmp> 
    		{
    		"source" : 组件的js对象,
    		"rowID" : 行Id,
    		"rowIndex" : 行索引
    		}
    		</xmp>	
    		*/
    		Model.dataSysInfoIndexChanged = function(event) {
    			if(event.rowID){
    				var systemFullID = $scope.dataSysInfo.getValue('systemFullID',event.rowID);
    				$scope.dataSysAction.filter = "SysAction.systemFullID='"+ systemFullID +"'";// AND sOrgKindID='ogn' AND sValidState=1 ";
    				$scope.dataSysAction.refreshData();
    			}
    		}
    		
    		/**
    		name:bizData#onDataChanged
    		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
    		@param {object} event 
    		<br/><b>结构如下：</b>
    		<xmp> 
    		{
    		"source" : 组件的js对象,
    		"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
    		"selfChanged" : 是否自身变化，false：从data触发的变化,
    		"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
    		}
    		</xmp>	
    		*/
    		Model.dataSysInfoDataChanged = function(event) {
    			
    		}
    		
    		/**
    		name:bizData#onValueChanged
    		description: <b>[回调型事件]</b>数据变化
    		@param {object} event 
    		<br/><b>结构如下：</b>
    		<xmp> 
    		{
    		"source" : 组件的js对象,
    		"column" : 关系,
    		"rowIndex" : 行索引,
    		"value" : 新值,
    		"originalValue" : 旧值
    		}
    		</xmp>	
    		*/
    		//输入值变化，监视input的输入，ng-change
    		Model.dataSysInfoValueChanged = function(event) {
    			
    		}
    		


    		/**
    		name:bizData#onBeforeNew
    		description: <b>[回调型事件]</b>业务数据新增前
    		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
    		*/
    		Model.dataSysInfoBeforeNew = function(event) {

    		};
    		
    		
    		/**
    		* name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
    		* 
    		* @param event
    		*            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
    		*/
    		Model.dataSysInfoAfterNew = function(event){

    		}
    		
       		
       		//刷新
       		Model.refresh = function() {
       			if($scope.appId==''){
       				alert('请选择服务商提供的软件系统');
       				return;
       			}
       	   		$scope.dataSysInfo.filter = "SysInfo.appId='"+$scope.appId+"'";

       			$scope.dataSysInfo.refreshData();
       		}
    		

    		///////////对象封装测试/////////////////////////
    		
    		var options = {};
    		options.container = Model;
    		options.concept = 'SysInfo';
    		options.name = 'dataSysInfo';
    		options.url = $rootScope.path;
    		options.creater = '';
    		options.httpUrl = $rootScope.getServiceName();
       		options.reader = '/backcenter/sysInfo/tree/query';
       		options.writer = '/backcenter/sysInfo/tree/save';
       		options.deleter = '/backcenter/sysInfo/tree/delete';
    		options.idColumnDefine = "fID";
    		options.idColumnName = "fID";
    		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},systemName:{name:"systemName",type:"String",label:""},systemFullID:{name:"systemFullID",type:"String",label:""},systemFullCode:{name:"systemFullCode",type:"String",label:""},systemFullName:{name:"systemFullName",type:"String",label:""},systemKind:{name:"systemKind",type:"String",label:""},systemDescribe:{name:"systemDescribe",type:"String",label:""},itemPrice:{name:"itemPrice",type:"Decimal",label:""},absoluteUrl:{name:"absoluteUrl",type:"String",label:""},nodeLevel:{name:"nodeLevel",type:"Integer",label:""},nodeKind:{name:"nodeKind",type:"String",label:""},sortNo:{name:"sortNo",type:"String",label:""},fParent:{name:"fParent",type:"String",label:""},status:{name:"status",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
    		options.isTree = true;
    		options.parentRelation = 'fParent';
    		options.nodeKindRelation = 'nodeKind';
    		options.nodeLevelRelation = 'nodeLevel';
    		options.nodeSortRelation = "sortNo";
       		options.rootFilter = 'SysInfo.fParent is null';
    		options.pathRelation = 'systemFullID';
    		options.directDeleteMode = false;
    		options.limit = -1;
    		options.orderBy = 'sortNo ASC';

    		options.onIndexChanging = Model.dataSysInfoIndexChanging;
    		options.onIndexChanged = Model.dataSysInfoIndexChanged;
    		options.onBeforeNew = null;
    		options.onAfterNew = Model.dataSysInfoAfterNew;
    		options.onBeforeDelete = null;
    		options.onAfterDelete = Model.dataSysInfoAfterDelete;
    		options.onValueChanged = Model.dataSysInfoValueChanged;
    		options.onDataChanged = Model.dataSysInfoDataChanged;
//                            		options.rowsWatcherFn = Model.rowsWatcherFn;
    		
    		$scope.dataSysInfo = new BizData(options);

//                            		$scope.dataSysInfo.deregIndex = $scope.$watch('dataSysInfo.currentIndex',$scope.dataSysInfo.watchCurrentIndex);
//                            		$scope.dataSysInfo.deregID = $scope.$watch('dataSysInfo.currentID',$scope.dataSysInfo.watchCurrentID);
    		//监视对象中的所有变动
    		$scope.dataSysInfo.deregRows = $scope.$watch('dataSysInfo.rows',$scope.dataSysInfo.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
//                            		$scope.dataSysInfo.deregCollection = $scope.$watch('dataSysInfo.rows',$scope.dataSysInfo.watchCollection,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
    		//////////////////////////////////////////////

    		
    		var options = {};
    		options.container = Model;
    		options.concept = 'SysAction';
    		options.name = 'dataSysAction';
    		options.url = $rootScope.path;
    		options.httpUrl = $rootScope.getServiceName();
    		options.creater = '';
    		options.reader = '/backcenter/sysAction/list/query';
    		options.writer = '/backcenter/sysAction/list/save';
    		options.deleter = '/backcenter/sysAction/list/delete';
    		options.idColumnDefine = "fID";
    		options.idColumnName = "fID";
    		options.defCols = {fID:{name:"fID",type:"String",label:""},systemFullID:{name:"systemFullID",type:"String",label:""},actionCode:{name:"actionCode",type:"String",label:""},actionName:{name:"actionName",type:"String",label:""},actionDescribe:{name:"actionDescribe",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
    		options.isTree = false;
    		options.parentRelation = 'sParent';
    		options.nodeKindRelation = 'sNodeKind';
    		options.nodeLevelRelation = 'sLevel';
    		options.nodeSortRelation = "sSequence";
    		options.pathRelation = 'sFID';
    		options.directDeleteMode = false;
    		options.orderBy = "actionCode ASC";
    		
    		$scope.dataSysAction = new BizData(options);

    		//监视对象中的所有变动
    		$scope.dataSysAction.deregRows = $scope.$watch('dataSysAction.rows',$scope.dataSysAction.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
   	   		
   	   		//加载树形数据
   	   		Model.refresh();
    		
//    		$scope.dataSysInfo.refreshData();
    		
    		

    		//添加(调用对话框)
    		Model.add = function() {
    			//打开对话框
				var systemFullID = $scope.dataSysInfo.getValue('systemFullID',$scope.dataSysInfo.getCurrentID());

				var options = {};
    			
    			options.recordState = 'new';
    			options.id = systemFullID;

    	        var modalInstance = $uibModal.open({  
    	        	backdrop: 'static',
    	        	size: '',
    	            templateUrl: $rootScope.getProjectName() + '/backcenter/sysAction/single',  
    	            controller: 'sysActionSingleCtrl as Model',  
    	            resolve: {
    	                params: function () {  
    	                    return angular.copy(options);  
    	                }
                        ,title : function() { return '新建'; }
                        ,loadCtrl : $ocLazyLoad.load('js/sysAction/single')
    		        }
    	        });  
    	        //模态窗口打开之后执行的函数
    	        modalInstance.opened.then(function(){  
    	        });  
    	        //模态窗口关闭之后执行的事件
    	        modalInstance.result.then(function (result) {  
    	             //成功关闭窗口
    	             $scope.dataSysAction.refreshData();
    	        }, function (reason) {  
    	        });  
    			
    			
    		};

    		//修改
    		Model.edit = function() {
    			//打开对话框
    			var index = $scope.dataSysAction.currentIndex;
    			var row = $scope.dataSysAction.rows[index];

    			var options = {};
    			
    			options.recordState = 'edit';
    			options.id = row.fID.value;

    	        var modalInstance = $uibModal.open({  
    	        	backdrop: 'static',
    	        	size: '',
    	            templateUrl: $rootScope.getProjectName() + '/backcenter/sysAction/single',  
    	            controller: 'sysActionSingleCtrl as Model',  
    	            resolve: {
    	                params: function () {  
    	                    return angular.copy(options);  
    	                }
                        ,title : function() { return '修改'; }
                        ,loadCtrl : $ocLazyLoad.load('js/sysAction/single')
    		        }
    	        });  
    	        //模态窗口打开之后执行的事件
    	        modalInstance.opened.then(function(){
    	        });  
    	        //模态窗口关闭之后执行的事件
    	        modalInstance.result.then(function (result) {  
    	             //成功关闭窗口
    	             $scope.dataSysAction.refreshData();
//    	             $scope.dataSysAction.currentIndex = index;
    	        }, function (reason) {  
    	        });  
    				
     		};


    		//（删除）
    		Model.triggerDeleteActionClick = function(){
    			var ids = new Array();
    			for(var index=0;index<$scope.dataSysAction.rows.length;index++){
    				if($scope.dataSysAction.rows[index].userdata.checked){
    					ids.push($scope.dataSysAction.rows[index].fID.value);
    				}
    			}
    			$scope.dataSysAction.deleteData(ids);
    			$scope.dataSysAction.refreshData();
    		}
    		
    		Model.templateAdd = function(billKind) {
    			//billKind：Bill，File
				var systemFullID = $scope.dataSysInfo.getValue('systemFullID',$scope.dataSysInfo.getCurrentID());

    			var data = {};//new Object();
    			data.billKind = billKind;
    			data.systemFullID = systemFullID;
    			
				var response = $.ajax({
		            type: "POST",
		            async : true,
		            url: $rootScope.getServiceName() + "/backcenter/sysAction/templateAdd",
		            data: JSON.stringify(data),// "{}",
		            contentType: "application/json; charset=utf-8",
		            dataType: "json",
//		            processData: false,
		            success: function (data,textStatus) {
		                // Play with returned data in JSON format
//		            	alert('添加成功！');
		            	console.log('data-->',data);
		            	var rowID = $scope.dataSysInfo.getCurrentID();
	    				var systemFullID = $scope.dataSysInfo.getValue('systemFullID',rowID);
	    				$scope.dataSysAction.filter = "SysAction.systemFullID='"+ systemFullID +"'";// AND sOrgKindID='ogn' AND sValidState=1 ";
	    				$scope.dataSysAction.refreshData();
	    				$scope.$apply();
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
		            }
		        });
   			 	
    		};
    		
    		//表格标题中的选择框ng-model
    		$scope.checkboxValue = false;
    		//表格标题中的选择对话框click事件
    		Model.checkboxClick = function () {  
    			for(var index=0;index<$scope.dataSysAction.rows.length;index++){
    				$scope.dataSysAction.rows[index].userdata.checked=$scope.checkboxValue;
    			}
    		};    
    		

    		
    		///////////////////////////页面级////////////////////////////////////////
    		//返回
    		Model.goBack = function(event) {
    			window.history.go(-1);
    		}
    		///////////////////////////页面级////////////////////////////////////////
    		
    		
    		return Model;
    		
    		}
   
    
})